package com.ljf.controller;

import com.ljf.annotation.HasPermission;
import com.ljf.dto.RoleQuery;
import com.ljf.entity.Role;
import com.ljf.exception.BusinessException;
import com.ljf.service.RoleService;
import com.ljf.util.Result;
import com.ljf.vo.RolePermissonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @GetMapping("/all")
    public Result getAllRole(){
        List<Map<String,Object>> list =  roleService.getAllRole();
        return Result.success(list);
    }
    @GetMapping("/queryRoleIdsByEid")
    public Result queryRoleIdsByEid(Long eid){
        List<Long> ids = roleService.getRoleIdsByEid(eid);
        return Result.success(ids);
    }

    @HasPermission("personnel_management:employee:queryRoleIdsByEid")
    @PostMapping("/saveRoleEmp")
    public Result saveRoleEmp(Long eid, Long[] empRoleIds, HttpServletRequest request) {
        String token = request.getHeader("token");
        roleService.saveRoleEmp(eid, empRoleIds, token);
        return Result.success();

    }
    /**
     * 查询角色所有的信息
     *
     * @return
     */
    @HasPermission("system:role:list")
    @PostMapping("/list")
    public Result list(RoleQuery qo) {
        List<Role> roles = roleService.listByQo(qo);
        return Result.success(roles);
    }



    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    @HasPermission("system:role:edit_role")
    @PostMapping("/edit_role")
    public Result edit_role(Role role) {
        if (Role.SYS_ID == role.getId() || 2L == role.getId()) {
            throw new BusinessException("不能停用系统拥有者");
        }
        if (role != null && StringUtils.hasText(role.getInfo())) {
            roleService.updateById(role);
        }
        return Result.success();
    }
    /**
     * 保存角色信息
     *
     * @param role
     * @return
     */
    @HasPermission("system:role:save")
    @PostMapping("/save")
    public Result save(Role role) {
        roleService.saveRole(role);
        return Result.success();
    }
    /**
     * 查询角色拥有的权限
     *
     * @param rid
     * @return
     */
    @HasPermission("system:role:saveRolePermissons")
    @GetMapping("/checkPermissons")
    public Result checkPermissons(@NotNull(message = "角色不能为空") Long rid) {
        RolePermissonVo vo = roleService.checkPermissons(rid);
        return Result.success(vo);
    }

    /**
     * 保存角色-菜单的关系
     *
     * @param rid
     * @param menuIds
     * @return
     */
    @HasPermission("system:role:saveRolePermissons")
    @PostMapping("/saveRolePermissons")
    public Result saveRolePermissons(@NotNull(message = "角色不能为空") @RequestParam("rid") Long rid, @RequestParam("menuIds") Long[] menuIds) {
        roleService.saveRolePermissons(rid, menuIds);

        return Result.success();

    }
}
